package com.cwt_taxi_xa.protocol;

import android.content.Context;
import android.util.Log;

import com.cwt_taxi_xa.MainApplication;
import com.cwt_taxi_xa.constants.MyStatus;
import com.cwt_taxi_xa.model.GpsLocationInfo;
import com.cwt_taxi_xa.service.SerialPortService;
import com.cwt_taxi_xa.util.GPSUtil;
import com.cwt_taxi_xa.util.Tools;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
 * 博海智能顶灯私有协议
 *
 * @author Administrator
 *
 */
public class DDUtil {

	private static StringBuffer tcp7EBuffer = new StringBuffer();
	private static Context context = MainApplication.getInstance()
			.getApplicationContext();

	// 行车状态：0x00-空车
	public static String order_kc = "7E 44 00 0C 00 00 44 52 49 56 00 00 00 3B A2 00 00 00 00 00 00 7E";
	// 行车状态：0x01-载客
	public static String order_zk = "7E 44 00 0C 00 00 44 52 49 56 01 00 00 0C 92 00 00 00 00 00 00 7E";
	// 行车状态：0x02-电召
	public static String order_dz = "7E 44 00 0C 00 00 44 52 49 56 02 00 00 55 C2 00 00 00 00 00 00 7E";
	// 行车状态：0x03-停运
	public static String order_ty = "7E 44 00 0C 00 00 44 52 49 56 03 00 00 62 F2 00 00 00 00 00 00 7E";
	// 行车状态：0x04-报警
	public static String order_bj = "7E 44 00 0C 00 00 44 52 49 56 04 00 00 E7 62 00 00 00 00 00 00 7E";
	// 行车状态：0x05-其他
	public static String order_qt = "7E 44 00 0C 00 00 44 52 49 56 05 00 00 D0 52 00 00 00 00 00 00 7E";

	public static String baojing =   "7E 43 00 0B 00 00 41 4C 41 52 00 05 F5 44 7E";
	public static String qxbaojing = "7E 43 00 09 00 00 55 41 4C 4D 6B 7A 7E";

	public static String[] orders = { order_kc, order_zk, order_dz, order_ty,
			order_bj, order_qt, baojing, qxbaojing };

	/**
	 * 当接收到顶灯上传数据，首先毫无保留地给服务器发一份，再判断是否本地需要处理
	 *
	 * @param data
	 */
	public static void onDDReceive(byte[] data) {

//		AppLog.e("顶灯数据："+Tools.parseByte2HexStr(data));

//		AppLog.saveTxt("收到串口一数据："+Tools.parseByte2HexStr(data));

//		// 这个数据包要上传到平台-------------------------------------------------------------
//		// 这个数据包要上传到平台-------------------------------------------------------------
//		// 这个数据包要上传到平台-------------------------------------------------------------
//		// 这个数据包要上传到平台-------------------------------------------------------------
//		DispatchCenterUsers.witeMsg(Encoder.getUplinkPassthrough(5, 12, 0, data), 0);

		NianBaoUtil.nbdd2(data);
//		NianBaoUtil.nbjjq(data, context);

	}

	/**
	 * 发送顶灯校时
	 */
	public static void sendDDtime(){
		String time = getDDsettime();
		if(time==null||!MyStatus.gps){
//			Log.e("sendDDtime", "未定位，顶灯校时失效");
			return;
		}else{
			String str = "430010000054494D45"+getDDsettime();
			str = str + CRCverify.getCRC16(Tools.parseHexStr2Byte(str));
			str = str.toUpperCase();
			str = Tools.parseByte2HexStr2(Tools.parseHexStr2Byte(str));
			str = str.replace(" 7D ", " 7D 01 ");
			str = str.replace(" 7E ", " 7D 02 ");
			str = " 7E "+str+" 7E ";
//			Log.e("sendDDtime", "顶灯校时数据"+str);
			SerialPortService.getInstance().sendSerialData1(Tools.parseHexStr2Byte(str));
		}


//		SerialPortService.getInstance().sendSerialData1("$$00000000000000110110019500000000000000爽爽的贵阳欢迎您AA&&".getBytes());
	}
	public static byte[] getDDtimeData(){
		String time = getDDsettime();
		String str = "430010000054494D45"+getDDsettime();
		str = str + CRCverify.getCRC16(Tools.parseHexStr2Byte(str));
		str = str.toUpperCase();
		str = Tools.parseByte2HexStr2(Tools.parseHexStr2Byte(str));
		str = str.replace(" 7D ", " 7D 01 ");
		str = str.replace(" 7E ", " 7D 02 ");
		str = " 7E "+str+" 7E ";
		return Tools.parseHexStr2Byte(str);
	}

	/**
	 * 发送星级设置
	 */
	public static void sendDDstar(int star){
		try {
			SerialPortService.getInstance().sendSerialData1(getDDstar(star));
			Log.e("sendDDtime", "顶灯星级设置"+star);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	/**
	 * 获取GPS时间
	 *
	 * @return
	 */
	public static String getDDsettime() {
		GpsLocationInfo loc = GPSUtil.getInstance().getLocInfo();
		if (loc != null) {
			long loctime = loc.getTime();
			if(loctime<0) return null;
			Date time = new Date(loctime);

			SimpleDateFormat sDateFormat = new SimpleDateFormat("yy-MM-dd-HH-mm-ss");
			Calendar c = Calendar.getInstance();
			c.setTime(time);

			String time_str = sDateFormat.format(time);
			String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));

			String[] ss = time_str.split("-");
			String result = "";
			for (int i = 0; i < 7; i++) {
				int t = 0;
				if(i<3){
					t = Integer.parseInt(ss[i]);
				}else if(i==3){
					t = Integer.parseInt(mWay);
				}else{
					t = Integer.parseInt(ss[i-1]);
				}
				String tt = Integer.toHexString(t);
				if(tt.length()==1) tt = "0"+tt;
				result+= tt;
			}
			return result;
		}
		return null;
	}


	/**
	 * 获取星级编码
	 *
	 * @param star
	 * @return
	 */
	public static byte[] getDDstar(int star) {
		String str = " 44 00 0C 00 00 45 56 41 4C 0" + star + " 00 00";
		str = str + CRCverify.getCRC16(Tools.parseHexStr2Byte(str));
		str = str.toUpperCase();
		str = str.replace("7D", "7D01");
		str = str.replace("7E", "7D02");
		str = "7E" + str + "0000000000007E";
		return Tools.parseHexStr2Byte(str);
	}

	/**
	 * 获取状态设置编码
	 *
	 * @param type
	 *            0空车 1载客 2电召 3停运 4报警 5其他 6被打劫状态 7取消被打劫状态
	 * @return
	 */
	public static byte[] getDDstatue(int type) {
		if(type==6){
			return "$$00S0FF0000测试数据&&".getBytes();
		}else if(type==7){
			return "$$00D123450FF0000测试数据&&".getBytes();
		}else{
			return Tools.parseHexStr2Byte(orders[type]);
		}
	}

	/**
	 * 判断数据帧是否需要本地处理
	 *
	 * @param data
	 */
	public static void isNeedCare(byte[] data) {
//		Log.e("DDUtil", "收到顶灯上传的完整数据包");
//		Log.e("DDUtil", Tools.parseByte2HexStr(data));

		// 这个数据包要上传到平台-------------------------------------------------------------
		// 这个数据包要上传到平台-------------------------------------------------------------
		// 这个数据包要上传到平台-------------------------------------------------------------
		// 这个数据包要上传到平台-------------------------------------------------------------
		DispatchCenterUsers.witeMsg(Encoder.getUplinkPassthrough(5, 253, 0, data), 0);

		//上电，判断gps是否定位，定位给校时
		//： 上电报告/申请校时指令的数据并非固定不变的，简单的解析方法：判断首尾是 0x7E，
		//第 7、8、9、10 的 4 个字节分别为 50 57 4F 4E
//		AppLog.e("接收顶灯数据:"+Tools.parseByte2HexStr2(data));
		if(Tools.parseByte2HexStr2(data).contains("50 57 4F 4E")){
//			AppLog.e("顶灯上电校时");
			sendDDtime();
		}

	}


	/**
	 * 顶灯状态 00:停运01:显示“空车” 02:报警，后面要取消的 03:显示“停运” 04:显示“电召” 05:显示“载客” 06:取消警报
	 */
	public static void sendText2TT(int i) {
		// TODO Auto-generated method stub
//		Log.e("调用顶灯状态设置", "状态-" + i);
//		AppLog.e("安卓发往顶灯（串口1）:"+i);
		switch (i) {
			case 0:
				SerialPortService.getInstance().sendSerialData1(Tools
						.parseHexStr2Byte(DDUtil.order_ty));
				break;
			case 1:
				SerialPortService.getInstance().sendSerialData1(Tools
						.parseHexStr2Byte(DDUtil.order_kc));
//			AppLog.e("验证频繁空重车--空车");
				break;
			case 2:
				SerialPortService.getInstance().sendSerialData1(Tools
						.parseHexStr2Byte(DDUtil.baojing));
				break;
			case 3:
				SerialPortService.getInstance().sendSerialData1(Tools
						.parseHexStr2Byte(DDUtil.order_ty));
				break;
			case 4:
				SerialPortService.getInstance().sendSerialData1(Tools
						.parseHexStr2Byte(DDUtil.order_dz));
				break;
			case 5:
				SerialPortService.getInstance().sendSerialData1(Tools
						.parseHexStr2Byte(DDUtil.order_zk));
//			AppLog.e("验证频繁空重车--重车");
				break;
			case 6:
				SerialPortService.getInstance().sendSerialData1(Tools
						.parseHexStr2Byte(DDUtil.qxbaojing));
				break;
		}

	}


}
